// RedJey Graphics User Interface Library for J2ME Platform.
// Copyright (C) 2008 Yevgen Krapiva
//
// This library is free software; you can redistribute
// it and/or modify it under the terms of the
// GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0
// of the License, or any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

package com.incrediblesoft.redjey;

/**
 * @author Yevgen Krapiva<br>
 *         Homepage: http://code.google.com/p/redjey/<br>
 *         Feedback: ykrapiva@gmail.com<br>
 *         Copyright (C) 2008 Yevgen Krapiva<br>
 */
import com.incrediblesoft.redjey.event.MouseEvent;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;

public class RadioButton extends Button
{
    protected CustomImage imageChecked, imageUnchecked;
    protected boolean checked;
    protected RadioButtonGroup group;

    public RadioButton(String title, boolean checked)
    {
        super(title);
        setChecked(checked);
    }

    public RadioButton(String title, boolean checked, String styleClassName)
    {
        super(title, styleClassName);
        setChecked(checked);
    }

    public RadioButton(String title, CustomImage imageUnchecked, CustomImage imageCheсked, boolean checked)
    {
        this(title, null, imageUnchecked, imageCheсked, checked);
    }

    public RadioButton(String title, String styleClassName, CustomImage imageUnchecked, CustomImage imageCheсked, boolean checked)
    {
        super(title, imageUnchecked, styleClassName);
        if (imageChecked != null)
            setCheckedImage(imageCheсked);
        if (imageUnchecked != null)
            setUncheckedImage(imageUnchecked);
        setChecked(checked);
    }

    public String getName()
    {
        return "RadioButton";
    }

    public void setCheckedImage(CustomImage image)
    {
        this.imageChecked = image;
        if (image != null) image.parent = this;
    }

    public void setUncheckedImage(CustomImage image)
    {
        this.imageUnchecked = image;
        if (image != null) image.parent = this;
    }

    public CustomImage getCheckedImage()
    {
        return imageChecked;
    }

    public CustomImage getUncheckedImage()
    {
        return imageUnchecked;
    }

    public void setState(int index)
    {
        super.setState(index);
        CustomImage image_checked = (CustomImage) currentStyle.attributes.get("image-checked");
        CustomImage image_unchecked = (CustomImage) currentStyle.attributes.get("image-unchecked");
        if (image_checked == null)
        {
            image_checked = createImage(true);
            if (image_checked != null)
                currentStyle.addAttribute("image-checked", image_checked);
        }
        if (image_unchecked == null)
        {
            image_unchecked = createImage(false);
            if (image_unchecked != null)
                currentStyle.addAttribute("image-unchecked", image_unchecked);
        }
        setCheckedImage(image_checked);
        setUncheckedImage(image_unchecked);
        setChecked(checked);
    }

    private CustomImage createImage(boolean checked)
    {
        CustomFont font = currentStyle.getFont();
        if (font == null) return null;

        Image image = Image.createImage(font.getHeight(), font.getHeight());
        Graphics g = image.getGraphics();
        g.setColor(font.getColor());
        g.drawRect(0, 0, image.getWidth()-1, image.getHeight()-1);
        if (checked)
        {
            g.drawLine(0, 0, image.getWidth()-1, image.getHeight()-1);
            g.drawLine(0, image.getHeight()-1, image.getWidth()-1, 0);
        }

        return new CustomImage(image);
    }

    public boolean isChecked()
    {
        return checked;
    }

    public void setChecked(boolean b)
    {
        checked = b;

        if (b)
            setImage(imageChecked);
        else
            setImage(imageUnchecked);

        repaint();        
    }

    protected void processMouseEvent(MouseEvent event)
    {
        switch(event.getID())
        {
            case MouseEvent.MOUSE_CLICKED:
                if (group != null)
                {
                    if (!checked)
                    {
                        setChecked(true);
                        group.notifyChecked(this);
                    }
                    else
                    break;
                }
                else setChecked(!isChecked());
                //repaint();
                break;
        }

        super.processMouseEvent(event);
    }

    public void setRadioButtonGroup(RadioButtonGroup group)
    {
        this.group = group;
        if (group != null)
            group.addRadioButton(this);
    }
}
